<?php

namespace iBrand\Shop\Backend\Console;

use Illuminate\Console\Command;
use DB;

class ShopBackendMenusCommand extends Command
{
	protected $signature = 'import:shop-backend-menus';

	protected $description = 'import shop backend menus';

	public function handle()
	{
		$lastOrder   = DB::table(config('admin.database.menu_table'))->max('order');
		$shopTopMenu = DB::table(config('admin.database.menu_table'))->where('title', '门店管理')->where('parent_id', 0)->first();
		if (!$shopTopMenu) {
			$shopTopMenuId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => 0,
				'order'      => $lastOrder++,
				'title'      => '门店管理',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 1,
				'uri'        => 'shop/list/store',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$shopTopMenuId = $shopTopMenu->id;
		}

		$shopManage = DB::table(config('admin.database.menu_table'))->where('title', '门店')->where('parent_id', $shopTopMenuId)->first();
		if (!$shopManage) {
			$shopManageId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '门店',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 1,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$shopManageId = $shopManage->id;
		}

		$shopList = DB::table(config('admin.database.menu_table'))->where('title', '门店列表')->where('parent_id', $shopManageId)->first();
		if (!$shopList) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopManageId,
				'order'      => $lastOrder++,
				'title'      => '门店列表',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/list/store',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		/*$tagManage = DB::table(config('admin.database.menu_table'))->where('title', '标签管理')->where('parent_id', $shopManageId)->first();
		if (!$tagManage) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopManageId,
				'order'      => $lastOrder++,
				'title'      => '标签管理',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/list/tag',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$activityManage = DB::table(config('admin.database.menu_table'))->where('title', '活动管理')->where('parent_id', $shopManageId)->first();
		if (!$activityManage) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopManageId,
				'order'      => $lastOrder++,
				'title'      => '活动管理',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/list/activity',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}*/

		$cityManage = DB::table(config('admin.database.menu_table'))->where('title', '城市管理')->where('parent_id', $shopManageId)->first();
		if (!$cityManage) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopManageId,
				'order'      => $lastOrder++,
				'title'      => '城市管理',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/list/city',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		/*$importShop = DB::table(config('admin.database.menu_table'))->where('title', '导入门店')->where('parent_id', $shopManageId)->first();
		if (!$importShop) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopManageId,
				'order'      => $lastOrder++,
				'title'      => '导入门店',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/import',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}*/

		$shopClerk = DB::table(config('admin.database.menu_table'))->where('title', '门店导购')->where('parent_id', $shopManageId)->first();
		if (!$shopClerk) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopManageId,
				'order'      => $lastOrder++,
				'title'      => '门店导购',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/list/clerk/all?status=1',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$shopSetting = DB::table(config('admin.database.menu_table'))->where('title', '基础设置')->where('parent_id', $shopTopMenuId)->first();
		if (!$shopSetting) {
			$shopSettingId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '基础设置',
				'icon'       => 'iconfont icon-xitongshezhi',
				'blank'      => 1,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$shopSettingId = $shopSetting->id;
		}

		$baseSetting = DB::table(config('admin.database.menu_table'))->where('title', '基本信息')->where('parent_id', $shopSettingId)->first();
		if (!$baseSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopSettingId,
				'order'      => $lastOrder++,
				'title'      => '基本信息',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/store/system',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$o2oSetting = DB::table(config('admin.database.menu_table'))->where('title', 'O2O设置')->where('parent_id', $shopSettingId)->first();
		if (!$o2oSetting) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopSettingId,
				'order'      => $lastOrder++,
				'title'      => 'O2O设置',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/store/clerk/settings',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$promotion = DB::table(config('admin.database.menu_table'))->where('title', '促销管理')->where('parent_id', $shopTopMenuId)->first();
		if (!$promotion) {
			$promotionId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '促销管理',
				'icon'       => 'iconfont icon-cuxiaoguanli',
				'blank'      => 1,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$promotionId = $promotion->id;
		}

		$discount = DB::table(config('admin.database.menu_table'))->where('title', '促销活动管理')->where('parent_id', $promotionId)->first();
		if (!$discount) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $promotionId,
				'order'      => $lastOrder++,
				'title'      => '促销活动管理',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/discount',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$coupon = DB::table(config('admin.database.menu_table'))->where('title', '优惠券管理')->where('parent_id', $promotionId)->first();
		if (!$coupon) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $promotionId,
				'order'      => $lastOrder++,
				'title'      => '优惠券管理',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/coupon',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$clerkManage = DB::table(config('admin.database.menu_table'))->where('title', '导购管理')->where('parent_id', $shopTopMenuId)->first();
		if (!$clerkManage) {
			$clerkManageId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '导购管理',
				'icon'       => 'iconfont icon-yuangongguanli',
				'blank'      => 1,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$clerkManageId = $clerkManage->id;
		}

		$clerkList = DB::table(config('admin.database.menu_table'))->where('title', '导购列表')->where('parent_id', $clerkManageId)->first();
		if (!$clerkList) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $clerkManageId,
				'order'      => $lastOrder++,
				'title'      => '导购列表',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/info/clerk/manage/list?status=1',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$shopOrder = DB::table(config('admin.database.menu_table'))->where('title', '门店订单')->where('parent_id', $shopTopMenuId)->first();
		if (!$shopOrder) {
			$shopOrderId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '门店订单',
				'icon'       => 'iconfont icon-dingdanguanli',
				'blank'      => 1,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$shopOrderId = $shopOrder->id;
		}

		$orderList = DB::table(config('admin.database.menu_table'))->where('title', '订单列表')->where('parent_id', $shopOrderId)->first();
		if (!$orderList) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopOrderId,
				'order'      => $lastOrder++,
				'title'      => '订单列表',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/info/order?shop_id=',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$refund = DB::table(config('admin.database.menu_table'))->where('title', '售后列表')->where('parent_id', $shopOrderId)->first();
		if (!$refund) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopOrderId,
				'order'      => $lastOrder++,
				'title'      => '售后列表',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/info/refund?shop_id=',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$userManage = DB::table(config('admin.database.menu_table'))->where('title', '用户管理')->where('parent_id', $shopTopMenuId)->first();
		if (!$userManage) {
			$userManageId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '用户管理',
				'icon'       => 'iconfont icon-jiaoseguanli',
				'blank'      => 1,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$userManageId = $userManage->id;
		}

		$userList = DB::table(config('admin.database.menu_table'))->where('title', '用户列表')->where('parent_id', $userManageId)->first();
		if (!$userList) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $userManageId,
				'order'      => $lastOrder++,
				'title'      => '用户列表',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/info/users?shop_id=',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$goodsManage = DB::table(config('admin.database.menu_table'))->where('title', '商品管理')->where('parent_id', $shopTopMenuId)->first();
		if (!$goodsManage) {
			$goodsManageId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $shopTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '商品管理',
				'icon'       => 'iconfont icon-shangpinshezhi-',
				'blank'      => 1,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$goodsManageId = $goodsManage->id;
		}

		$goodsList = DB::table(config('admin.database.menu_table'))->where('title', '商品列表')->where('parent_id', $goodsManageId)->first();
		if (!$goodsList) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $goodsManageId,
				'order'      => $lastOrder++,
				'title'      => '商品列表',
				'icon'       => '',
				'blank'      => 1,
				'uri'        => 'shop/info/goods?sold_out=ACTIVITY',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

        $pointDeductionSetting = DB::table(config('admin.database.menu_table'))->where('title', '积分抵扣设置')->where('parent_id', $goodsManageId)->first();
        if (!$pointDeductionSetting) {
            DB::table(config('admin.database.menu_table'))->insertGetId([
                'parent_id'  => $goodsManageId,
                'order'      => $lastOrder++,
                'title'      => '积分抵扣设置',
                'icon'       => '',
                'blank'      => 1,
                'uri'        => 'shop/info/goods/pointDeductionSetting',
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time()),
            ]);
        }


        $brand = DB::table(config('admin.database.menu_table'))->where('title', '品牌管理')->where('parent_id', $goodsManageId)->first();
        if (!$brand) {
            DB::table(config('admin.database.menu_table'))->insertGetId([
                'parent_id'  => $goodsManageId,
                'order'      => $lastOrder++,
                'title'      => '品牌管理',
                'icon'       => 'shop/info/brand',
                'blank'      => 1,
                'uri'        => 'shop/info/brand',
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time()),
            ]);
        }
	}
}